package org.zxp.guava;

import com.google.common.base.Charsets;
import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;

/**
 * @program: effectiveJava
 * @description: MurmurHash测试
 * @author: X-Pacific zhang
 * @create: 2019-08-02 10:59
 **/
public class TestMurmurHash {
    /**
     * 这两种写法得出的结果一致
     * @param args
     */
    public static void main(String[] args){
        User user = new User("zxp","1");
        HashFunction hf = Hashing.murmur3_128(); // x64
//        HashFunction hf = Hashing.murmur3_32(); //x32
        HashCode hc = hf.hashObject(user, (from, into) -> {
            into.putString(from.getName(), Charsets.UTF_8);
            into.putString(from.getId(), Charsets.UTF_8);
        });
        System.out.println(hc.toString());


        String hashcode = Hashing.murmur3_128().newHasher().putString(user.getName(), Charsets.UTF_8).putString(user.getId(), Charsets.UTF_8).hash().toString();
        System.out.println(hashcode);
    }

    static class User{
        public User(String name, String id) {
            this.name = name;
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        private String name;
        private String id;
    }
}
